//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm;
using EntLibContrib.Resource;
using EntLibContrib.Resource.Configuration;

namespace EntLibContrib.Resource.Configuration.Manageability
{
	internal class DataResourceProviderDataManageabilityProvider
		: ConfigurationElementManageabilityProviderBase<DataResourceProviderData>
	{
		#region Constants
		public const String DatabaseInstanceNamePropertyName = "databaseInstanceName";
		public const String ResourceBaseNamePropertyName = "resourceBaseName";
		#endregion

		#region Properties
		protected override string ElementPolicyNameTemplate
		{
			get
			{
				return null;	// no policy for these elements
			}
		}
		#endregion

		#region Methods
		protected override void AddAdministrativeTemplateDirectives(AdmContentBuilder contentBuilder,
			DataResourceProviderData configurationObject,
			IConfigurationSource configurationSource,
			String elementPolicyKeyName)
		{
			// parts for providers are part of their resource manager's policies
			AddElementAdministrativeTemplateParts(contentBuilder,
				configurationObject,
				configurationSource,
				elementPolicyKeyName);
		}

		protected override void AddElementAdministrativeTemplateParts(AdmContentBuilder contentBuilder, 
			DataResourceProviderData configurationObject, 
			IConfigurationSource configurationSource, 
			String elementPolicyKeyName)
		{
			contentBuilder.AddEditTextPart(InternalResources.DataResourceProviderDataDatabaseInstanceNamePartName,
				elementPolicyKeyName,
				DatabaseInstanceNamePropertyName,
				configurationObject.DatabaseInstanceName,
				255,
				true);

			contentBuilder.AddEditTextPart(InternalResources.DataResourceProviderDataResourceBaseNamePartName,
				elementPolicyKeyName,
				ResourceBaseNamePropertyName,
				configurationObject.ResourceBaseName,
				255,
				true);
		}

		protected override void OverrideWithGroupPolicies(DataResourceProviderData configurationObject, IRegistryKey policyKey)
		{
			String resourcePathNameOverride = policyKey.GetStringValue(DatabaseInstanceNamePropertyName);
			String resourceBaseNameOverride = policyKey.GetStringValue(ResourceBaseNamePropertyName);

			configurationObject.DatabaseInstanceName = resourcePathNameOverride;
			configurationObject.ResourceBaseName = resourceBaseNameOverride;
		}

		protected override void GenerateWmiObjects(DataResourceProviderData configurationObject,
			ICollection<ConfigurationSetting> wmiSettings)
		{
			wmiSettings.Add(
				new DataResourceProviderSetting(configurationObject.Name,
					configurationObject.DatabaseInstanceName,
					configurationObject.ResourceBaseName));
		}
		#endregion
	}
}
